package com.whpu.mall.search.controller;

import com.whpu.common.exception.BizCodeEnum;
import com.whpu.common.to.es.SkuEsModel;
import com.whpu.common.utils.R;
import com.whpu.mall.search.service.ElasticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("search/save")
@RestController
public class ElasticSaveController {

    @Resource
    private ElasticSaveService elasticSaveService;

    /**
     * 上架商品数据到es
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean hasFailures = true;
        try {
            hasFailures = elasticSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("Elastic上架商品数据到es失败, {}",e);
        }
        return hasFailures?R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage()):R.ok();
    }
}
